package quickup;

import java.io.File;

import javax.swing.SwingUtilities;

public class Test implements ProgressListener {
	private static final long serialVersionUID = 1L;
	private static int lastPos = 0;
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {new Test();}}
		);
	}
	
	public Test() {
		File f = new File("C:\\myfile.jpg");
		HttpDestination d = new HttpDestination("http://www.example.com/test.cgi");
		JSONObject j = null;
		try {
			j = new JSONObject("{var1:fred,var2:bob}");
		} catch (JSONException je) {
			System.out.println(je.getMessage());
		}
		HttpFileUploader u = new HttpFileUploader(f,"uploadfile",j,d);
		u.addProgressListener(this);
		u.start();
	}

	public void onProgressChange(Object origin, int position, int total) {
		if(position > lastPos + 100000) {
			double prog = (double)position / total;
			double perc = prog * 100;
			long percent = Math.round(perc);
			System.out.println(percent + "%");			
			lastPos = position;
		}
	}

	public void onStatusChange(Object origin, String status) {
		System.out.println(status);		
	}

	public void onOperationFinished(Object origin) {
		// TODO Auto-generated method stub
	}
}
